import { createApp } from 'vue'
// 路由
import router from './router'

//pinia
import { createPinia } from 'pinia'
import piniaPluginPersist from 'pinia-plugin-persist'
const pinia = createPinia()
pinia.use(piniaPluginPersist)

// element 3.0
import ElementPlus from 'element-plus'
import * as ElementPlusIconsVue from '@element-plus/icons-vue'
import 'element-plus/theme-chalk/src/index.scss'

// 基础样式
import './assets/styles/base.scss'
// 公共样式
import './assets/styles/common.scss'

// 事件总线
import eventBus from 'vue3-eventbus'

import App from './App.vue'

// 乾坤
import { renderWithQiankun, qiankunWindow } from 'vite-plugin-qiankun/dist/helper'
import actions from '@/utils/action'

let app: any
if (!qiankunWindow.__POWERED_BY_QIANKUN__) {
  app = createApp(App)
  // 引入ElementUI
  app.use(ElementPlus, { zIndex: 1000, namespace: 'ep' })
  app.use(eventBus)
  app.use(router).use(pinia).mount('#app')
  for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
    app.component(key, component)
  }
} else {
  renderWithQiankun({
    mount(props: any) {
      if (props) {
        actions.setActions(props)
      }
      app = createApp(App)
      // 引入ElementUI
      app.use(ElementPlus, { zIndex: 1000, namespace: 'ep' })
      app.use(eventBus)
      app.use(router).use(pinia).mount(props.container.querySelector('#app'))
      for (const [key, component] of Object.entries(ElementPlusIconsVue)) {
        app.component(key, component)
      }
    },
    bootstrap() {
      console.log('vue app bootstrap')
    },
    update() {
      console.log('vue app update')
    },
    unmount() {
      console.log('vue app unmount')
      app?.unmount()
    },
  })
}
// const app = createApp(App)
